import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Exam{
    Map<String, Object> account = new HashMap<String, Object>();
    public Exam(String id){
        this.account=FileOpera.JsonRead(id);
    }

    public void exam(Scanner input){
        System.out.println("======进入答题界面======");
        System.out.println("1.填空题");
        System.out.println("2.选择题");
        System.out.println("3.退出");
        int choose = input.nextInt();
        if(choose==3){return ;}
        else if(choose==1){
            doExercises(input,"tiankong");
        }
        else if(choose==2){
            doExercises(input,"xuanze");
        }

        else{
            System.out.println("当前输入的操作不存在!");
        }

        //除了返回操作,其他操作完成后依旧返回此界面
        exam(input);

    }



    public void doExercises(Scanner input, String type){
        System.out.println("===正在生成题目===");
        int scores=0;
        //读取题目总数
        int index_max=FileOpera.getFileNums("problems",type);
        for(int i=1;i<=5;i++)
        {
            //随机生成题目
            int index=(int)(Math.random()*index_max)+1;
            //读取题目的内容，每一行存入List
            List<String> problem = FileOpera.ReadLines("problems",type, index+"");
            //输出题目的内容
            int lines=problem.size();
            String ans=problem.get(lines-1);
            System.out.print(i+". ");
            for(int j=0;j<lines-1;j++)
            {
                System.out.println(problem.get(j));
            }
            System.out.println("请输入你的答案:");
            String yourAnswer = input.next();
            //判断答案是否正确
            if(!yourAnswer.equals(ans))
            {
                System.out.println("回答错误!");
                System.out.println("正确答案是: " + ans);
                //把错误加入错题本
                FileOpera.writeAll((String) this.account.get("id"),"error",problem);
            }
            else{
                System.out.println("回答正确!");
                scores+=20;
            }
        }
        //答题结束,算出分数
        System.out.println("");
        System.out.println("答题结束!");
        System.out.println("你的得分: "+scores);
        this.account.put("正确题数",(int)this.account.get("正确题数")+scores/20);
        FileOpera.JsonWrite((String) this.account.get("id"),this.account);
    }


}
